﻿using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Timers;
using System.Xml.Serialization;
using Microsoft.Win32;
using WoodRobotics.Weather.ModelView;

namespace WoodRobotics.Weather.ViewModel
{
    
    public class CurrentWeather : INotifyPropertyChanged
    {
        
        #region Constructor
        public CurrentWeather()
        {
            
            m_UpdateTimer = new Timer();
            m_UpdateTimer.Interval = 30000;
            m_UpdateTimer.Elapsed += new ElapsedEventHandler(m_UpdateTimer_Elapsed);
            m_UpdateTimer.Start();
            m_UpdateTimer_Elapsed(null, null);
            
            Astronomy = new AstronomyEvents();
            
        }
        #endregion
        
        #region Fields
        private IP m_IP;
        private IPtoGeo m_IP2Geo;
        private Timer m_UpdateTimer;
        #endregion
        
        #region Properties
        public string ZipCode
        {
            get
            {
                if (Registry.CurrentUser.CreateSubKey(@"Software\WoodRobotics\Weather").GetValueNames().Contains("ZipCode"))
                    return (string)Registry.CurrentUser.CreateSubKey(@"Software\WoodRobotics\Weather").GetValue("ZipCode");
                else
                {
                    
                    m_IP = new IP();
                    m_IP.BeginIPLookup(new AsyncCallback(IP_Responce));
                    
                    ZipCode = "43015";
                    
                    return "43015";
                    
                }
            }
            set 
            { 
                Registry.CurrentUser.CreateSubKey(@"Software\WoodRobotics\Weather").SetValue("ZipCode", value);
                m_UpdateTimer_Elapsed(null, null);
            }
        }
        public Weather.ModelView.Weather Weather { get; set; }
        public AstronomyEvents Astronomy { get; set; }
        #endregion
        
        #region Events
        private void m_UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            m_UpdateTimer.Stop();
            UpdateWeather();
        }
        private void WeatherResponse(IAsyncResult Responce)
        {
            
            HttpWebRequest l_WebRequest = (HttpWebRequest)Responce.AsyncState;
            HttpWebResponse l_WebResponce = (HttpWebResponse)l_WebRequest.EndGetResponse(Responce);
            
            this.Weather = ((xml_api_reply)new XmlSerializer(typeof(xml_api_reply)).Deserialize(new StreamReader(l_WebResponce.GetResponseStream()))).Weather;
            
            NotifyPropertyChanged("Weather");
            
            m_UpdateTimer.Start();
            
        }
        private void IP_Responce(IAsyncResult Responce)
        {
            
            m_IP2Geo = new IPtoGeo()
            {
                IpAddress = m_IP.IpInfo.ip_address
            };
            m_IP2Geo.BeginQueryGeoIP(new AsyncCallback(GeoIP_Responce));
            
        }
        private void GeoIP_Responce(IAsyncResult Responce)
        {
            ZipCode = m_IP2Geo.GeoInfo.Location.CityData.postal_code;
            NotifyPropertyChanged("ZipCode");
        }
        #endregion
        
        #region Methods
        private void UpdateWeather()
        {
            HttpWebRequest l_WebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + ZipCode);
            l_WebRequest.Method = "GET";
            l_WebRequest.ContentType = "application/x-www-form-urlencoded";
            l_WebRequest.BeginGetResponse(new AsyncCallback(WeatherResponse), l_WebRequest);
        }
        #endregion
        
        #region INotifyPropertyChanged Members
        private void NotifyPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        
    }
    
}